package com.gs.cd.service.resource.aop;

import com.alibaba.fastjson.JSON;
import com.gs.cd.api.ApiResult;
import com.gs.cd.service.resource.api.ResourceApi;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Arrays;

/**
 * @Author seven
 * @Date 2020/12/25 16:45
 * @Description
 * @Version 1.0
 */
@Aspect
@Component
@Slf4j
public class ResourceApiExceptionAOP {

    @Pointcut("execution(public * com.gs.cd.service.resource.api.ResourceApi.*(..))")
    public void pointCut() {
    }

    @Around("pointCut()")
    public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
        Object proceed = null;
        // 获取方法名称
        String methodName = joinPoint.getSignature().getName();
        //获取目标类
        Object target = joinPoint.getThis();
        // 获取入参
        Object[] param = joinPoint.getArgs();
        try {
            proceed = joinPoint.proceed();
            return proceed;
        } catch (Throwable throwable) {
            throwable.printStackTrace();
            log.error(String.format("请求资源服务错误：请求地址：%s，执行方法：%s，参数：%s，执行结果：%s",
                    ResourceApi.getUrl(), methodName, Arrays.toString(param), proceed));
            throw throwable;
        }
    }

}
